<?php
/*
# ------------------------------------------------------------------------
# JA Job Board Package
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com.
# ------------------------------------------------------------------------
*/


defined('_JEXEC') or die();

jimport( 'joomla.application.component.model' );

class JobBoardModelJAPosts extends JModel
{
	
	var $_data;
	var $cate;
	function __construct()
	{
		parent::__construct();

		$array = JRequest::getVar('cid',  0, '', 'array');
		$this->setId((int)$array[0]);
		$this->cate =null;
	}
	function setId($id)
	{
		// Set id and wipe data
		$this->_id		= $id;
		$this->_data	= null;
	}


	function &getItem()
	{
		// Load the data
		if (empty( $this->_data )) {
			$query = ' SELECT * FROM #__ja_posting_plans '.
					'  WHERE id = '.(int)$this->_id;
			$this->_db->setQuery( $query );
			$this->_data = $this->_db->loadObject();
		}
		if (!$this->_data) {
			$this->_data = new stdClass();
			$this->_data->id = 0;
		}
		return $this->_data;
	}
	
	function &getTable(){
		 if($this->_table == null){
        	$this->_table = &JTable::getInstance('JA_PostingPlans', 'JobBoardTable');
		}
		return $this->_table;
	}	
	
	function getTable_plan_invoice(){
		 if($this->_table_invoice == null){
        	$this->_table_invoice = &JTable::getInstance('JA_PostingPlans_invoice', 'JobBoardTable');
		}
		return $this->_table_invoice;
	}
	
	function getTable_employer(){
		 if($this->_table_employer == null){
        	$this->_table_employer = &JTable::getInstance('JA_Employer', 'JobBoardTable');
		}
		return $this->_table_employer;
	}
	
	// get all subscriptions
	function getItems()
	{		
		$db	=& JFactory::getDBO();
				
		$query = 'SELECT  s.* FROM #__ja_posting_plans AS s '
				 .'WHERE s.published=1 '
				 .'ORDER BY ordering desc';
				 
		$db->setQuery($query);		
		$items = $db->loadObjectList();
		for ($i=0;$i<count($items);$i++)		
		{
			if ($items[$i]->type=='resume'&&$items[$i]->value==-1)
				$items[$i]->temp_value = 'Lifetime';
			elseif (($items[$i]->type=='standard'||$items[$i]->type=='premium')&&$items[$i]->value==-1)
				$items[$i]->temp_value = 'Unlimited';
			else 
				$items[$i]->temp_value = $items[$i]->value;
		}
		$types = JobBoardConstant::getPostingPlan_Type();
		
		$groups = array();
		if ($types) {
			foreach ($types as $k=>$type){
				$groups[] = $k;
			}
		}
		
		$list = array();
		if ($items) {
			foreach ($items as $k=>$item){
				if(in_array($item->type, $groups)){
					$list[$item->type][] = $item;
				}
			}
		}
		
		return $list;
	}
	
	function create_invoice_posting_plan($user_id=0, $product, $status='', $method='manual', $currency='credit'){
		global $jbconfig;
		
		$row = $this->getTable_plan_invoice();				
		
		$max_invoice_id = (int)$this->get_max_invoice_id();
		if(!$max_invoice_id) $max_invoice_id = $jbconfig['billing']->invoice_id_start;
		else $max_invoice_id++;			
		
		$data = array();		
	    $data['invoice_id'] = $max_invoice_id;
	    $data['invoice_date'] = date("Y-m-d H:i:s");	    
	    $data['status'] = $status;
	    $data['user_id'] = $user_id;
	    $data['posting_plan_id'] = $product->id;
	    if($product->type=='standard')	    $data['standard_posts_quantity'] = $product->value;
	    if($product->type=='premium')		$data['premium_posts_quantity'] = $product->value;
	    if($product->type=='resume')		$data['search_resume_quantity'] = $product->value;
	    $data['amount'] = $product->price;
	    $data['payment_method'] = $method;
	    	    
	    $data['currency_code'] = $currency;

		if (!$row->bind( $data )) {
			echo "<script> alert('".$row->getError(true)."'); window.history.go(-1); </script>\n";
			return JText::_("Bind data false");
		}
		
		if ($row->check() != 'SUCCESS')
		{
			echo "<script> alert('".$row->getError(true)."'); window.history.go(-1); </script>\n";
			return JText::_('Check data failed');
		}
		
		if (!$row->store())
		{
			echo "<script> alert('".$row->getError(true)."'); window.history.go(-1); </script>\n";
			return JText::_('Store fail');
			//return false;
		}		
		return $row;		
	}		
	
	function getPackage_credits(){
	
		$db = JFactory::getDBO();
		$sql = "select * from #__ja_credits where published=1";
		$db->setQuery($sql);
		return $db->loadObjectList();
	
	}
		
}